<template>
  <div class="header">
    <el-header>
      <h1 class="title">
        <i class="el-icon-shopping-cart-full" style="margin-right: 10px"></i>
        超市管理系统
      </h1>
      <!-- 当前登陆用户 -->
      <div class="username">
          <span class="el-dropdown-link">
            {{username}}
          </span>
        <!--默认是hover显示-->
        <el-dropdown>
          <!--头像-->
          <div class="avatar"></div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>
              <span @click="person">个人中心</span>
            </el-dropdown-item>
            <el-dropdown-item>
              <span @click="person" v-if="this.$store.state.isIndex === false">返回首页</span>
            </el-dropdown-item>
            <el-dropdown-item>
              <span @click="exitLogin">退出登录</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <!-- 用户头像 -->
      </div>
    </el-header>
  </div>
</template>
<script>
export default {
  data() {
    return {
      username:'',
    };
  },
  methods: {
    getUsername(){
      this.username = window.sessionStorage.getItem('username')
    },
    person() {
      this.$router.push("/personal");
    },
    exitLogin() {
      window.sessionStorage.removeItem('token'); //拿掉token 和 username
      window.sessionStorage.removeItem('username');
      this.$router.push('/login'); // 返回登录界面。
    }
  },
  created() {
      this.getUsername();
  },
};
</script>
<style lang="less">
.header {
  .el-header {
    position: relative;
    height: 60px;
    line-height: 60px;
    //text-align: right;
    background-color:#4d638c;
    color: #fff;
    border-bottom: 1px solid rgba(0, 0, 0, 0.2);
    .title {
      margin: 0;
      line-height: 60px;
      position: absolute;
    }
    // 用户名
    .username {
      height: 60px;
      line-height: 60px;
      margin-right: 20px;
      display: flex;
      // 右对齐
      float: right;
      align-items: center;
      // 头像
      .avatar {
        width: 50px;
        height: 50px;
        margin-left: 20px;
        line-height: 60px;
        border-radius: 50%;
        text-align: center;
        background-image: url("../assets/head.jpg");
        background-size: cover;
      }
    }
  }
}
</style>